/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-15
*/
package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.*;
import com.rzico.basics.model.ShopVo;
import com.rzico.basics.service.*;
import com.rzico.core.entity.SysUser;
import com.rzico.core.service.SysUserService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.exception.CustomException;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 名厨档案控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-12-15
 */
@Api(description = "名厨档案接口")
@RestController
@RequestMapping("/chef")
public class ChefController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ChefService chefService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ChefLaudService chefLaudService;
    /**
     * 分页查询名厨档案
     *
     * @return
    */
    @ApiOperation("分页查询名厨档案")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名字", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "meta", value = "菜系", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "beGoodAt", value = "擅长", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "shopId", value = "店铺ID", dataType = "String", paramType = "query")
    })
    public CommResult<Chef> list(String name, String meta, String beGoodAt,Long shopId, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }
        if (StringUtils.isNotEmpty(meta)) {
            params.put("meta", meta);
        }
        if (StringUtils.isNotEmpty(beGoodAt)) {
            params.put("beGoodAt", beGoodAt);
        }

        if (shopId!=null) {
            Shop shop = shopService.selectByPrimaryKey(shopId);
            params.put("enterpriseId",shop.getEnterpriseId());
        } else {
            SysUser admin = sysUserService.findByUsername("admin");
            Enterprise enterprise = enterpriseService.findByMch(admin.getMchId());
            params.put("enterpriseId",enterprise.getId());
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Chef> list = chefService.selectList(params);
        PageResult<Chef> pageResult = new PageResult<Chef>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条名厨档案
     *
     * @return
    */
    @ApiOperation("查询单条名厨档案")
    @GetMapping("/find/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lat", value = "lat", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "lng", value = "lng", dataType = "Double", paramType = "query"),
    })
    public CommResult<Chef> find(@PathVariable String id,Double lat,Double lng) {
        Member member = memberService.getCurrent();
        Chef result = chefService.findById(id);
        if (result == null) {
            throw new CustomException("厨师数据不存在");
        }
        result.setFollowFlag(false);
        Map<String,Object> data = new HashMap<>();
        data.put("chef",result);
        if(member != null) {
            data.put("memberId",member.getId());
        }
        data.put("chefId",result.getId());
        List<ChefLaud> chefLauds = chefLaudService.selectList(data);
        data.remove("chefId");
        if (chefLauds != null && chefLauds.size() == 1) {
            result.setFollowFlag(true);
        }


        Enterprise enterprise = enterpriseService.selectByPrimaryKey(result.getEnterpriseId());
        if (lat==null) {
            lat = 0d;
        }
        if (lng==null) {
            lng = 0d;
        }

        Shop shop = shopService.findByPoint(enterprise,lat,lng);
        if (shop!=null) {
            Map<String, Object> params = new HashMap<String, Object>();

            params.put("id", shop.getId());

            List<ShopVo> list = shopService.selectLbsList(params, lat, lng);

            if (list.size() > 0) {
                data.put("shopVo", list.get(0));
            }

        }
        return CommResult.success(data);

    }

}
